package com.sxsihe.heartmate;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import cn.pedant.SweetAlert.widget.SweetAlertDialog;
import com.sxsihe.heartmate.util.HttpPostUtil;
import com.sxsihe.heartmate.util.PersistentCookieStore;
import com.sxsihe.heartmate.util.SharePreferenceUtil;
import com.sxsihe.heartmate.view.CustomWaittingDialog;
import org.apache.http.cookie.Cookie;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;

public class UserLoginActivity extends Activity implements View.OnClickListener {
    Button btn_login;
    Button href_register;
    Button get_back_pwd;
    EditText login_input_username;
    EditText login_input_password;
    CustomWaittingDialog dialog;

    SharePreferenceUtil spu;
    public void onCreate(Bundle bundle){
        super.onCreate(bundle);
        setContentView(R.layout.login);
        if (spu == null) {
            spu = SharePreferenceUtil.getInstance(this);
        }
        dialog = new CustomWaittingDialog(this,R.anim.connect_waitting);
        PersistentCookieStore cookieStore = new PersistentCookieStore(this);
        btn_login = (Button)findViewById(R.id.btn_login);
        href_register = (Button)findViewById(R.id.href_register);
        get_back_pwd = (Button)findViewById(R.id.get_back_pwd);
        login_input_username = (EditText)findViewById(R.id.login_input_username);
        login_input_password = (EditText)findViewById(R.id.login_input_password);
         Iterator<Cookie> iter = cookieStore.getCookies().iterator();
        while (iter.hasNext()){
            Cookie cookie = iter.next();
            if(cookie.getName().equals("loginname")&&cookie.getDomain().equals(spu.get("server_ip", ""))){
                login_input_username.setText(cookie.getValue());
            }else if(cookie.getName().equals("password")&&cookie.getDomain().equals(spu.get("server_ip",""))){
                login_input_password.setText(cookie.getValue());
            }
        }
        btn_login.setOnClickListener(this);
        href_register.setOnClickListener(this);
        get_back_pwd.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_login:
//                dialog.showWaitting();
//                if(spu.get("server_ip","").equals("")){
//                    dialog.hideWaitting();
//                    new SweetAlertDialog(this, SweetAlertDialog.ERROR_TYPE)
//                            .setTitleText("出错了")
//                            .setContentText("请填入正确的服务地址")
//                            .show();
//                    return;
//                }
//                String url = "http://"+spu.get("server_ip","");
//                if(spu.get("server_port","").equals("")){
//                    url +=":"+spu.get("server_port","");
//                }
//                url +="/zhcjhy/loginAction.do?action=mobilelogin";
//                String args [] = {url,login_input_username.getText().toString(),login_input_password.getText().toString()};
//                HttpPostUtil post = new HttpPostUtil(this,"login");
//                post.execute(args);
                finish();
                break;
            case R.id.href_register:
//                intent = new Intent(UserLoginActivity.this,UserRegisterActivity.class);
                Intent intent = new Intent(UserLoginActivity.this,UserRegister2Activity.class);
                startActivity(intent);
                finish();
                break;
            case R.id.get_back_pwd:
                intent = new Intent(UserLoginActivity.this,GetBackPwdActivity.class);
                startActivity(intent);
                finish();
                break;
        }

    }

    public void startUserInfoActivity(){
        Intent intent = new Intent(UserLoginActivity.this,MainActivity.class);
        startActivity(intent);
        finish();

    }

    public void loginResult(String result){
        dialog.hideWaitting();
        if ("".equals(result)){
            new SweetAlertDialog(this, SweetAlertDialog.ERROR_TYPE)
                    .setTitleText("出错了")
                    .setContentText("通讯异常")
                    .show();
        }else{
            try {
                JSONObject json = new JSONObject(result);
                if(json.getBoolean("success")){
                    JSONObject msg = json.getJSONObject("msg");
                    startUserInfoActivity();
                }else{
                    new SweetAlertDialog(this, SweetAlertDialog.ERROR_TYPE)
                            .setTitleText("出错了")
                            .setContentText("用户名密码不匹配")
                            .show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

}
